/**Util module holds helper services for all Apps like backend services 
 * that knows how to connect using different protocols Get/Post...
 **/

var utils = angular.module('utils', []);

utils.service('backendSrv',['$http', backendService])

function backendService($http){

   this.sendCmd = function(path, cmd, cb, cbErr){
       $http({method: 'POST', url: path,
           headers: { accept: "application/json" }
           ,data: cmd
       }).success(function(data, status, headers, config){
               cb(data);
       }).error(
           //function(data){window.alert("server error: " + data)}
           cbErr
           );
   };

   this.getTemplate = function(templateUID, cb){
       $http({method: 'GET', url: '/template/'+templateUID 
           ,headers: { accept: 'application/json'}
           //,data: {templateUID: templateUID} 
       })
           .success(function(data, status, headers, config){
               console.log('data returned: ');
               console.dir(data);
               cb(data);
       }).error(function(data){window.alert("server error: " + data)});

   }

   this.listDocs = function(cb){
       $http({method: 'GET', url: '/docs/list' 
           ,headers: { accept: 'application/json'}
           //,data: {id: id} 
       })
           .success(function(data, status, headers, config){
               console.log('data returned: ');
               console.dir(data);
               cb(data);
       }).error(function(data){window.alert("server error: " + data)});
   }


   this.executeRemoteCmd = function(cmd, data, returnType, renderer){
       //$http.post("/docker/exec", cmd).success(function(data, status, headers, config){
        
       $http({method: 'POST', url: '/docker/list',
           headers: { accept: returnType }
           ,data: data
       })
           .success(function(data, status, headers, config){
               renderer(cmd, data);
       }).error(function(data){window.alert("server error: " + data)});
   };

   this.cmd = function(url, data, cb){
       $http({method: 'POST', url: url,
           headers: { accept: 'json' }
           ,data: data
       })
           .success(function(data, status, headers, config){
               cb(data);
       }).error(function(data){window.alert("server error: " + data)});
   };
}
